
/**
 * Android Helper.
 *
 * Released under Apache License, Version 2.0.
 */

package bsp.android;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;

//@SuppressWarnings("unchecked")
public class AndroidHelper
{	
	protected AndroidHelper()
	{
		/* empty by design */
	}
	
	/**
	 * Sleep for specific milliseconds.
	 * 
	 * @param duration
	 */
	public static void sleepSomeTime(int duration)
		throws InterruptedException
	{
		try {
			Thread.sleep(duration);
		} catch ( InterruptedException ex ) {
			throw ex;
		}
	}
	
	/**
	 * Sleep for specific milliseconds.
	 * 
	 * @param duration
	 */
	public static void sleepSomeTimeConsumeInterruptedException(int duration)
	{
		try {
			Thread.sleep(duration);
		} catch ( InterruptedException ex ) {
			// nothing here by design
		}
	}
	
	/**
	 * Show toast message.
	 * 
	 * @param context
	 * @param text
	 * @param length
	 */
	public static void showToast(Context context, String text, int length)
	{
		Toast.makeText(context, text, length).show();
	}

	/**
	 * Show toast message for short duration.
	 * 
	 * @param context
	 * @param text
	 */
	public static void showShortToast(Context context, String text)
	{
		showToast(context, text, Toast.LENGTH_SHORT);
	}

	/**
	 * Show toast message for long duration.
	 * 
	 * @param context
	 * @param text
	 */
	public static void showLongToast(Context context, String text)
	{
		showToast(context, text, Toast.LENGTH_LONG);
	}

	/**
	 * Get height of the status bar.
	 * 
	 * @param view
	 * @return
	 */
	public static int getStatusBarHeight(View view)
	{
		// get the size of the status bar that is displayed at the top
		Rect rect = new Rect();
		view.getWindowVisibleDisplayFrame(rect);
		
		return rect.top;
	}
	
	/**
	 * Get size of the visible display frame.
	 * 
	 * @param view
	 * @return
	 */
	public static Rect getVisibleDisplayFrameSize(View view)
	{
		// get the size of the status bar that is displayed at the top
		Rect rect = new Rect();
		view.getWindowVisibleDisplayFrame(rect);
		
		return rect;
	}
	
	/**
	 * Get resource ID.
	 * 
	 * @param activity
	 * @param strName
	 * @param type
	 * @return
	 */
	public static int getResourceID(Activity activity, String strName, String type)
	{
		int retVal = activity.getResources().getIdentifier(strName, type, activity.getPackageName());
		
		return retVal;
	}
	
	@SuppressWarnings("deprecation")
	public static int getSDKVersion()
	{
		int retVal;
		
		// for Android 1.5 and above
		retVal = new Integer(android.os.Build.VERSION.SDK).intValue();
		// for Android 1.6 and above
		//retVal = new Integer(android.os.Build.VERSION.SDK_INT).intValue();
		return retVal;
	}
	
	public static String getReleaseVersion()
	{
		return android.os.Build.VERSION.RELEASE;
	}
	
	@SuppressWarnings("deprecation")
	public static String getBuildDetails()
	{
		StringBuffer buf = new StringBuffer();

	    buf.append("VERSION.RELEASE:" + android.os.Build.VERSION.RELEASE);
	    buf.append(",VERSION.INCREMENTAL:" + android.os.Build.VERSION.INCREMENTAL);
	    buf.append(",VERSION.SDK:"+android.os.Build.VERSION.SDK);
	    buf.append(",BOARD:"+android.os.Build.BOARD);
	    buf.append(",BRAND:"+android.os.Build.BRAND);
	    buf.append(",DEVICE:"+android.os.Build.DEVICE);
	    buf.append(",FINGERPRINT:"+android.os.Build.FINGERPRINT);
	    buf.append(",HOST:" + android.os.Build.HOST);
	    buf.append(",ID:" + android.os.Build.ID);

	    return buf.toString(); 
	}
	
	/**
	 * Is small device.
	 * @param context
	 * @return
	 */
	public static boolean isSmallDevice(Context context)
	{
	    boolean retVal = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL);
	    return retVal;
	}
	
	/**
	 * is normal device.
	 * @param context
	 * @return
	 */
	public static boolean isNormalDevice(Context context)
	{
		boolean retVal = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL);
		return retVal;
	}

	/**
	 * Is large device.
	 * @param context
	 * @return
	 */
	public static boolean isLargeDevice(Context context)
	{
	    boolean retVal = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
	    return retVal;
	}
	
	/**
	 * is X large device.
	 * @param context
	 * @return
	 */
	public static boolean isXLargeDevice(Context context)
	{
		boolean retVal = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
		return retVal;
	}
	
	public static final int SCREEN_SIZE_NOT_SET = 0;
	public static final int SCREEN_SIZE_CANNOT_DETERMINE = 1;
	public static final int SCREEN_SIZE_SMALL = 2;
	public static final int SCREEN_SIZE_NORMAL = 3;
	public static final int SCREEN_SIZE_LARGE = 4;
	public static final int SCREEN_SIZE_EXTRA_LARGE = 5;
	
	// Determine screen size
	public static int getScreenSize(Configuration config)
	{
		int screenSize = SCREEN_SIZE_NOT_SET;
		
		if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {     
			//Toast.makeText(this, "Extra Large screen",Toast.LENGTH_LONG).show();
			screenSize = SCREEN_SIZE_EXTRA_LARGE;
		} else if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {     
			//Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
			screenSize = SCREEN_SIZE_LARGE;
		} else if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {     
			//Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show();
			screenSize = SCREEN_SIZE_NORMAL;
		} else if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {     
			//Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show();
			screenSize = SCREEN_SIZE_SMALL;
		} else {
			//Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
			screenSize = SCREEN_SIZE_CANNOT_DETERMINE;
		}

	    return screenSize;
	}
	
	/**
	 * Checks if the device is a tablet or a phone.
	 * @param activityContext The Activity Context.
	 * @return Returns true if the device is a Tablet
	 */
	public static boolean isTabletDevice(Context activityContext)
	{
	    // Verifies if the Generalized Size of the device is XLARGE to be
	    // considered a Tablet
	    boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout & 
	                        Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);

	    // If XLarge, checks if the Generalized Density is at least MDPI
	    // (160dpi)
	    if ( xlarge ) {
	        DisplayMetrics metrics = new DisplayMetrics();
	        Activity activity = (Activity) activityContext;
	        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

	        // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
	        // DENSITY_TV=213, DENSITY_XHIGH=320
	        if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
	                || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
	                || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
	                || metrics.densityDpi == DisplayMetrics.DENSITY_TV
	                || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
	            // Yes, this is a tablet!
	            return true;
	        }
	    }

	    // No, this is not a tablet!
	    return false;
	}
	
	public static final int DENSITY_NOT_SET = 0;
	public static final int DENSITY_NOT_DETERMINED = 1;
	public static final int DENSITY_LOW = 2;
	public static final int DENSITY_MEDIUM = 3;
	public static final int DENSITY_HIGH = 4;
	
	//Determine density
	public static int getDisplayDensty(Resources res)
	{
		int retVal;
		
		retVal = DENSITY_NOT_SET;
		
		DisplayMetrics metrics = res.getDisplayMetrics();
		int density = metrics.densityDpi;

		if ( density == DisplayMetrics.DENSITY_HIGH ) {
			//Toast.makeText(this, "DENSITY_HIGH... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
			retVal = DENSITY_HIGH;
		} else if (density==DisplayMetrics.DENSITY_MEDIUM) {
			//Toast.makeText(this, "DENSITY_MEDIUM... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
			retVal = DENSITY_MEDIUM;
		} else if (density==DisplayMetrics.DENSITY_LOW) {
			//Toast.makeText(this, "DENSITY_LOW... Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
			retVal = DENSITY_LOW;
		} else {
			//Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW.  Density is " + String.valueOf(density),  Toast.LENGTH_LONG).show();
			retVal = DENSITY_NOT_DETERMINED;
		}
		
		return retVal;
	}
	
	/**
	 * This method convets dp unit to equivalent device specific value in pixels. 
	 * 
	 * @param dp A value in dp(Device independent pixels) unit. Which we need to convert into pixels
	 * @param context Context to get resources and device specific display metrics
	 * @return A float value to represent Pixels equivalent to dp according to device
	 */
	public static float convertDpToPixel(float dp,Context context)
	{
		Resources resources = context.getResources();
		DisplayMetrics metrics = resources.getDisplayMetrics();
		float px = dp * (metrics.densityDpi/160f);
	    return px;
	}
	
	/**
	 * This method converts device specific pixels to device independent pixels.
	 * 
	 * @param px A value in px (pixels) unit. Which we need to convert into db
	 * @param context Context to get resources and device specific display metrics
	 * @return A float value to represent db equivalent to px value
	 */
	public static float convertPixelsToDp(float px,Context context)
	{
	    Resources resources = context.getResources();
	    DisplayMetrics metrics = resources.getDisplayMetrics();
	    float dp = px / (metrics.densityDpi / 160f);
	    return dp;
	}
}

/* end */
